package com.alienlab.utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import sun.launcher.resources.launcher;

public class HttpUtil {
	  public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
	    public static final MediaType XML = MediaType.parse("application/xml; charset=utf-8");
	    private static HttpUtil instance;
	    private OkHttpClient client;
	 
	    private HttpUtil() {
	        client = new OkHttpClient();
	        client.setConnectTimeout(20, TimeUnit.SECONDS);//设置请求超时，20s
	        client.setReadTimeout(20, TimeUnit.SECONDS);//设置读取超时，20s
	        client.setWriteTimeout(20, TimeUnit.SECONDS);//设置写入超时，20s
	    }
	 
	    public static HttpUtil getInstance() {
	        return instance == null ? instance = new HttpUtil() : instance;
	    }
	 
	    public static void main(String[] args) throws Exception {
	        String post = HttpUtil.getInstance().get("https://leancloud.cn:443/1.1/classes/tb_news?order=news_no&&","X-LC-Id", "04tA7v3k4qyxjytIPrXDULgq-gzGzoHsz","X-LC-Key","laSjFrKQD7Bvf991kvcD8Sps","Content-Type", "application/json");
	        String post2 = HttpUtil.getInstance().get("https://leancloud.cn:443/1.1/classes/news_pdf?where=%7B%22news_time%22%3A%222016-06%2F07%22%7D&&&order=news_no&&","X-LC-Id", "04tA7v3k4qyxjytIPrXDULgq-gzGzoHsz","X-LC-Key","laSjFrKQD7Bvf991kvcD8Sps","Content-Type", "application/json");
	        JSONObject jsonObject  = JSONObject.parseObject(post);
	        JSONObject jsonObject2 = JSONObject.parseObject(post2);
	        JSONArray jsonArray  = jsonObject.getJSONArray("results");
	        JSONArray jsonArray2 = jsonObject2.getJSONArray("results");
	        for(int i  = 0 ; i<jsonArray.size() ; i ++){
	        	JSONObject jsonObject3 = jsonArray.getJSONObject(i);
	        	String jString = jsonObject3.getString("news_no");
	        	int y = 0;
	        	for(int j = 0 ; j<jsonArray2.size() ;j++ ){
	        		JSONObject jsonObject4 = jsonArray.getJSONObject(j);
	        		String jString_ = jsonObject4.getString("news_no");
	        		if(jString.equals(jString_)){
	        			y++;
	        		}
	        	}
	        	if(y == 1){
	        		
	        	}else{
	        		System.out.println("没有下载的报纸编号"+jString);
	        	}
	        }
	    }
	 
	    /**
	     * 提交POST请求
	     *
	     * @param url       地址
	     * @param mediaType 请求体文本类型
	     * @param content   请求体
	     * @param headers   请求头
	     * @return
	     * @throws IOException
	     */
	    public String post(String url, MediaType mediaType, String content, String... headers) throws IOException {
	        RequestBody body = RequestBody.create(mediaType, content);
	        Request.Builder builder = new Request.Builder().url(url);
	        if (headers != null && headers.length > 0 && headers.length % 2 == 0) {
	            for (int i = 0; i < headers.length - 1; i++)
	                if (i % 2 == 0) builder.addHeader(headers[i], headers[i + 1]);
	        }
	        Request request = builder.post(body).build();
	        
	        Response response = client.newCall(request).execute();
	        
	        return response.body().string();
	    }
	 
	    /**
	     * 提交POST请求
	     *
	     * @param url     地址
	     * @param content 请求体
	     * @return
	     * @throws IOException
	     */
	    public String post(String url, String content) throws IOException {
	        return post(url, HttpUtil.JSON, content, "X-LC-Id", "04tA7v3k4qyxjytIPrXDULgq-gzGzoHsz","X-LC-Key","laSjFrKQD7Bvf991kvcD8Sps","Content-Type", "application/json");
	    }
	 
	    /**
	     * 提交GET请求
	     *
	     * @param url 地址
	     * @return
	     * @throws IOException
	     */
	    public String get(String url, String... headers) throws IOException {
	    	Request.Builder builder = new Request.Builder().url(url);
	    	 if (headers != null && headers.length > 0 && headers.length % 2 == 0) {
		            for (int i = 0; i < headers.length - 1; i++)
		                if (i % 2 == 0) builder.addHeader(headers[i], headers[i + 1]);
		        }
	        Request request = builder.build();
	        Response response = client.newCall(request).execute();
	        return response.body().string();
	    }
}
